package com.pig4cloud.pig.trip.Service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.trip.Service.TrTripService;
import com.pig4cloud.pig.trip.api.dto.InquireDTO;
import com.pig4cloud.pig.trip.api.dto.TripDTO;
import com.pig4cloud.pig.trip.api.entity.TrTrip;
import com.pig4cloud.pig.trip.api.feign.SysRoleFeign;
import com.pig4cloud.pig.trip.mapper.TrTripMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

import static cn.hutool.core.date.DateTime.now;

@Service
@AllArgsConstructor
public class TrTripServiceImpl  extends ServiceImpl<TrTripMapper, TrTrip> implements TrTripService {

	@Autowired
    private  TrTripMapper trTripMapper;

	@Autowired
	 private SysRoleFeign sysRoleFeign;

	@Override
	public List<InquireDTO> getUnusedInquireList() {

		return trTripMapper.getUnusedInquireList(SecurityUtils.getUser().getId());
	}

	@Override
	public IPage<TripDTO> selectByCondition(Page page, String inquireNumber, String cusTel, String trStatus) {
		// 获取当前用户角色编码
		List<Long> roles = SecurityUtils.getRoles();
		String isService=null;
	    Long id=roles.get(0);
		if(sysRoleFeign.getById(id).getData().getRoleCode().equals("ROLE_SERVICE")){
			isService = "0";
		}
		return trTripMapper.selectByCondition(page, inquireNumber, cusTel, trStatus,isService);
	}

	@Override
	public InquireDTO getInquireBYId(Long inquireId) {
		return trTripMapper.getInquireBYId(inquireId);
	}
	@Override
	public int insertTrip(TrTrip trTrip) {
		//随机生成行程单编号谁8位
		trTrip.setTripNumber(IdUtil.fastSimpleUUID().substring(0,6));
		//删除标志初始值设置为0
		trTrip.setDelFlag('0');
		//设置创建时间
		trTrip.setCreateTime(now());
		//设置由谁创建
		trTrip.setCreateBy(SecurityUtils.getUser().getName());
		//设置由谁更新
		trTrip.setUpdateBy(SecurityUtils.getUser().getName());
		return trTripMapper.insertTrip(trTrip);
	}

	@Override
	public int deleteTrip(Long tripId) {
		return trTripMapper.deleteTrip(tripId);
	}

	@Override
	public InquireDTO getInquireByTripId(Long tripId) {
		return trTripMapper.getInquireByTripId(tripId);
	}

	@Override
	public int updateTripAmount(TripDTO tripDTO) {
		return trTripMapper.updateTripAmount(tripDTO);
	}
}
